
package ru.kpm.rectpack.represent;

import java.awt.Rectangle;
import javax.media.j3d.Group;
import javax.media.j3d.Texture;

/**
 *
 * @author Podorov Alexander
 */

public class Container3DModel extends Model3D
{
    public Container3DModel(InputData inputData, OutputData outputData)
    {
        super(inputData, outputData);
    }

    protected Group createPalletPacking(float packHeight, float palletHeight, float heightAboveFloor, int layers, Texture wood, Texture pack)
    {
        Group root = new Group();        
        Rectangle palletRect = null;
        Rectangle packRect = null;
        
        for (int i = 0; i < outputData.getPallets().size(); i++)
        {
            palletRect = outputData.getPallets().get(i);
            root.addChild(createPallet((float)palletRect.getWidth() * SCALE, (float)palletRect.getHeight() * SCALE, palletHeight, (float)(palletRect.getMinX() * SCALE) + THICKNESS, (float)(palletRect.getMinY() * SCALE) + THICKNESS, heightAboveFloor, wood));

            for (int k = 0; k < layers; k++)
            {
                for (int j = 0; j < outputData.getPacks().get(i).size(); j++)
                {
                    packRect = outputData.getPacks().get(i).get(j);
                    root.addChild(createBox((float)packRect.getWidth() * SCALE, (float)packRect.getHeight() * SCALE, packHeight, (float)((packRect.getMinX() + palletRect.getMinX()) * SCALE) + THICKNESS, (float)((packRect.getMinY() + palletRect.getMinY()) * SCALE) + THICKNESS, heightAboveFloor + palletHeight + packHeight * k, pack));
                }
            }

            root.addChild(createPallet((float)palletRect.getWidth() * SCALE, (float)palletRect.getHeight() * SCALE, palletHeight, (float)(palletRect.getMinX() * SCALE) + THICKNESS, (float)(palletRect.getMinY() * SCALE) + THICKNESS, heightAboveFloor + palletHeight + packHeight * layers, wood));
        }        

        return root;
    }

    protected Group createContent()
    {
        Group root = new Group();        

        root.addChild(createAxises(X_AXIS, inputData.getCx() * SCALE, inputData.getCy() * SCALE, inputData.getCz() * SCALE));
        root.addChild(createAxises(Y_AXIS, inputData.getCx() * SCALE, inputData.getCy() * SCALE, inputData.getCz() * SCALE));
        root.addChild(createAxises(Z_AXIS, inputData.getCx() * SCALE, inputData.getCy() * SCALE, inputData.getCz() * SCALE));

        root.addChild(createBox(THICKNESS, inputData.getCy() * SCALE + THICKNESS, inputData.getCz() * SCALE + THICKNESS, 0.0f, 0.0f, 0.0f, metal.getTexture()));
        root.addChild(createBox(inputData.getCx() * SCALE + THICKNESS, THICKNESS, inputData.getCz() * SCALE + THICKNESS, 0.0f, 0.0f, 0.0f, metal.getTexture()));
        root.addChild(createBox(inputData.getCx() * SCALE + THICKNESS, inputData.getCy() * SCALE + THICKNESS, THICKNESS, 0.0f, 0.0f, 0.0f, metal.getTexture()));

        root.addChild(createPalletPacking(inputData.getZ() * SCALE, inputData.getDpz() * SCALE, THICKNESS, inputData.getLayers1(), wood.getTexture(), pack.getTexture()));
        root.addChild(createPalletPacking(inputData.getZ() * SCALE, inputData.getDpz() * SCALE, THICKNESS + 2 * inputData.getDpz() * SCALE + inputData.getLayers1() * inputData.getZ() * SCALE, inputData.getLayers2(), wood.getTexture(), pack.getTexture()));

        return root;
    }

    protected float[] getTransformation()
    {
        return new float[] {-0.6940432f, 0.7198228f, -0.012616328f, 0.0f, -0.2553476f, -0.22974204f, 0.9391572f, 0.0f, 0.67312825f, 0.6550372f, 0.34325594f, -3.0f, 0.0f, 0.0f, 0.0f, 1.0f};
    }
}
